<template>
  <div>
    <div class="app-container clearfix pageHeader">
      <div class="header fl" style="padding: 22px 0;">{{$route.meta.title}} - {{name}}</div>
      <div class="extra fr">
        <el-alert
          :title="currentAuditStatus.text"
          :type="currentAuditStatus.type"
          show-icon
          :closable="false"
          v-if="true">
        </el-alert>
      </div>
    </div>
    <el-card shadow="never" class="app-container margin-content no-padding">
      <div slot="header" class="clearfix">
        <div class="header fl">吊装费用明细（包括吊运、托运费用）（单位：元）</div>
        <div class="extra fr">
          <el-button type="primary" @click="handleUpdate({costType:1})" v-if="auditStatus !== 1">新增</el-button>
        </div>
      </div>
      <el-table
        :key="liftTableKey"
        :data="liftTableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;">

        <el-table-column label="码头作业代码" property="actType"/>
        <el-table-column label="码头作业名称" property="name"/>
        <el-table-column label="20尺所需费用" property="twentyCost"/>
        <el-table-column label="40尺所需费用" property="fortyCost"/>
        <el-table-column label="操作" v-if="auditStatus !== 1">
          <template slot-scope="scope">
            <a @click="handleUpdate({id:scope.row.id})">编辑</a>
            <divider type="vertical"/>
            <a @click="handleRemove(scope.row.id)">删除</a>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card shadow="never" class="app-container margin-content no-padding">
      <div slot="header" class="clearfix">
        <div class="header fl">移位费用明细（包括短距离运输、拆箱、装箱费用）（单位：元）</div>
        <div class="extra fr">
          <el-button type="primary" @click="handleUpdate({costType:2})" v-if="auditStatus !== 1">新增</el-button>
        </div>
      </div>

      <el-table
        :key="moveTableKey"
        :data="moveTableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;">

        <el-table-column label="码头作业代码" property="actType"/>
        <el-table-column label="码头作业名称" property="name"/>
        <el-table-column label="20尺所需费用" property="twentyCost"/>
        <el-table-column label="40尺所需费用" property="fortyCost"/>
        <el-table-column label="操作" v-if="auditStatus !== 1">
          <template slot-scope="scope">
            <a @click="handleUpdate({id:scope.row.id})">编辑</a>
            <divider type="vertical"/>
            <a @click="handleRemove(scope.row.id)">删除</a>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card shadow="never" class="app-container margin-content no-padding">
      <div slot="header" class="clearfix">
        <div class="header fl">仓储费用明细（单位：元/天）</div>
        <div class="extra fr">
          <!--<el-button type="primary" @click="handleUpdate({costType:3})">新增</el-button>-->
        </div>
      </div>

      <el-table
        :key="storageTableKey"
        :data="storageTableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;">

        <el-table-column label="码头作业代码" property="actType"/>
        <el-table-column label="码头作业名称" property="name"/>
        <el-table-column label="20尺所需费用" property="twentyCost"/>
        <el-table-column label="40尺所需费用" property="fortyCost"/>
        <el-table-column label="操作" v-if="auditStatus !== 1">
          <template slot-scope="scope">
            <a @click="handleUpdate({id:scope.row.id})">编辑</a>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div class="toolbar clearfix" v-if="false">
      <div class="fl"></div>
      <div class="fr">
        <el-button type="primary">保存</el-button>
      </div>
    </div>

    <add-or-update ref="addOrUpdate" v-if="addOrUpdateVisible" @refresh="_getAll"></add-or-update>
  </div>
</template>

<script>
  // api
  import { del, getAll } from '@/api/costSubsidies'
  // vuex
  import { mapGetters,mapMutations } from 'vuex'
  // components
  import Divider from '@/components/Divider'
  import AddOrUpdate from './components/add-or-update'

  const MAP = {
    0: {
      text: '未审核',
      type: 'warning'
    },
    1: {
      text: '已审核',
      type: 'success'
    },
    2: {
      text: '退回',
      type: 'danger'
    }
  }

  export default {
    name: 'costSetting',
    data() {
      return {
        liftTableKey: 0,
        moveTableKey: 1,
        storageTableKey: 2,
        tableData: [],
        loading: false,

        auditStatus: 0,
        btnShow: true,
        showFlag: false,
        addOrUpdateVisible: false
      }
    },
    computed: {
      liftTableData() {
        return this.tableData.filter((data) => {
          return data.costType === 1
        })
      },
      moveTableData() {
        return this.tableData.filter((data) => {
          return data.costType === 2
        })
      },
      storageTableData() {
        return this.tableData.filter((data) => {
          return data.costType === 3
        })
      },
      currentAuditStatus() {
        return MAP[this.auditStatus]
      },
      ...mapGetters([
        'name'
      ])
    },
    created() {
      this._getAll()
    },
    methods: {
      _getAll() {
        this.loading = true
        getAll().then((res) => {
          this.tableData = res.data.data
          if (this.tableData.length > 0) {
            this.auditStatus = this.tableData[0].auditStatus
          }
          this.loading = false
        })
      },
      handleUpdate({ id, costType }) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.setCurrentId({ id: id, costType: costType })
          this.$refs.addOrUpdate.init()
        })
      },
      handleRemove(id) {
        this.$confirm(`确定对该项进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          del({ ids: id }).then(() => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this._getAll()
              }
            })
          })
        }).catch(() => {
        })
      },
      ...mapMutations({
        setCurrentId: 'SET_CURRENTID'
      })
    },
    components: {
      Divider,
      AddOrUpdate
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss" scoped>
  .el-card__header {
    padding: 10px;
    .header {
      padding: 2px 0;
    }
    .extra {
      padding: 0 !important;
    }
  }
</style>
